//
//  main.m
//  字符串截取
//
//  Created by wenjim on 17/10/22.
//  Copyright © 2017年 WenJim. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    
    NSString * str = @"<head>ZBC 爱してる</head>";
    /*
    // NSRange ：位置／长度
//    NSRange range = {6,3};
//    NSRange range ;
//    range.location = 6;
//    range.length = 8;
    // 只要是OC提供的结构体，一般都可以使用NSMakeXXX来创建
//    NSRange range = NSMakeRange(6, 3);
    */
    
    /*
    // 1.动态获取截取的起始位置
    NSUInteger location = [str rangeOfString:@">"].location + 1;
    // 2.动态获取截取的长度
    // 注意：rangeOfString 是从左至右的开始查找，只要找到就不找了
//    NSUInteger length = [str rangeOfString:@"<" options:NSBackwardsSearch].location - location;
    NSUInteger length = [str rangeOfString:@"</"].location - location;
    NSLog(@"location = %lu,length = %lu",location,length);
    
    NSRange range = NSMakeRange(location, length);
    NSString * newStr = [str substringWithRange:range];
    NSLog(@"str = %@",str);
    NSLog(@"newStr = %@",newStr);
    */
    
    // 验证rangeOfString 是从左至右开始查找，只要找到就不找了
    /*
    NSString * temp = @"abcdefga";
    NSRange range = [temp rangeOfString:@"a" options:NSBackwardsSearch];
    NSLog(@"location = %lu",range.location);
     */
    
    // 从什么地方开始截取，一直截取到最后
//   NSString * newStr =  [str substringFromIndex:6];
    // 从开头开始截取，一直截取到什么位置
    NSString * newStr = [str substringToIndex:6];
    
//    NSLog(@"newStr = %@",newStr);
    
    /*
     <head>ZBC 爱してる</head> --> ZBC 爱してる</head> --> ZBC 爱してる
     <head>ZBC 爱してる</head> --> <head>ZBC 爱してる --> ZBC 爱してる
     */
    NSLog(@"str = %@",str);
    NSUInteger location = [str rangeOfString:@">"].location + 1;
    NSString * str2 = [str substringFromIndex:location];
    NSLog(@"str2 = %@",str2);
    
    location = [str2 rangeOfString:@"</"].location;
    str2 = [str2 substringToIndex:location];
    NSLog(@"str2 = %@",str2);
    
    
    
    NSUInteger location2 = [str rangeOfString:@"</"].location;
    NSString * str3 = [str substringToIndex:location2];
    NSLog(@"str3 = %@",str3);
    
    location2 = [str3 rangeOfString:@">"].location +1;
    str3 = [str3 substringFromIndex:location2];
    NSLog(@"str3 = %@",str3);
    
    
    return 0;
}
